package com.aldobucchi.mxmlx
{
	import mx.containers.HBox;
	import mx.core.ContainerCreationPolicy;
	import mx.core.IFlexDisplayObject;


	/**
	 * Read the following article
	 * https://riastuff-trac.cvsdude.com/flexlib/wiki/ConditionalMXML
	 * 
	 * 
	 * @author aldo.bucchi@gmail.com
	 * 
	 * 
	 */
	public class Switch extends HBox
	{
		
		/**
		* for internal debugging
		*/		
		private static var serialID:int=0;
		
		/**
		* for debugging
		*/		
		private var instanceName:String = "Switch" + (serialID++);
		
		
		public function Switch()
		{
			// we shouldn't be using a container...
			// patch solution: make it use zero space
			setStyle( "paddingTop" , 0 );
			setStyle( "paddingRight" , 0 );
			setStyle( "paddingBottom" , 0 );
			setStyle( "paddingLeft" , 0 );
		}
		
		
		
		override public function get creationPolicy():String
		{
			return ContainerCreationPolicy.NONE;
		}
		
		
		
		/**
		* a reference to the current contained component so we can destroy it when needed
		*/		
		private var component:IFlexDisplayObject;
		
	
		private var _selectedIndex:int = -1;
		
		public function get selectedIndex():int
		{
			fyi( "::get selectedIndex " + _selectedIndex );
			return _selectedIndex;
		}
		/**
		 * if changed at runtime, for now we are doing nothing
		 * we can try sth later...
		 * 
		 * @param value
		 * 
		 */		
		public function set selectedIndex( value:int ):void
		{
			fyi( "::set selectedIndex " + value );
			
			var changed:Boolean = ( selectedIndex != value );
			_selectedIndex = value;
			
			if ( changed )
				createComponent();
		}

		
		/**
		 * This is the function that actually does all the work.
		 * It Is called whenever the selectedIndex is set, 
		 * but will always wait till createComponentsFromDescriptors() has been called at least once
		 */		
		private function createComponent():void
		{
			fyi( "::createComponent, selectedIndex " + selectedIndex );			
			
			if ( childDescriptors.length == 0 )
				return; // no children specified
			
			// TODO: when we implement runtime changing of the index
			// remove current children
			
			if ( selectedIndex != -1 && childDescriptors[ selectedIndex ] )
			{
				fyi( "::createComponentFromDescriptor, selectedIndex " + selectedIndex + " !!!" );
				component = createComponentFromDescriptor( childDescriptors[ selectedIndex ] , true );
			}
			
			
			validateNow();
		}

		private function fyi( msg:String ):void
		{
			// trace( instanceName + " " + msg );		
		}
		
		

	}
	
	
}

/*
 
 		override public function createComponentsFromDescriptors( recurse:Boolean = true ):void
		{
			fyi( "::createComponentsFromDescriptors, selectedIndex " + selectedIndex );
			
			recurse = recurse;
			
			createComponentsFromDescriptorsHasBeenCalledAtLeastOnce = true;
			
			if ( selectedIndex != -1 ) // we might need to wait till the selectedindex has been set
			
				createComponent();
			
		} 
 
 
 */